package com.wx.activitys.admin.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.base.utils.WebUtil;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.EnumUtils.WithdrawStateType;
import com.wx.activitys.drp.model.BCard;
import com.wx.activitys.drp.model.BWithdrawal;
import com.wx.activitys.drp.modelExample.BWithdrawalExample;
import com.wx.activitys.drp.service.BCardService;
import com.wx.activitys.drp.service.BWithdrawalService;
import com.wx.activitys.xmlInterf.aipg.rtreq.Trans;
import com.wx.activitys.xmlInterf.allinpay.xmltrans.demo.TranxMain;

@Controller
@RequestMapping("/withdrawal")
public class WithdrawalController {
	@Autowired
	private BWithdrawalService bWithdrawalService;
	@Autowired
	private BCardService bCardService;

	@RequestMapping(value = "list.html", method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) {
		BWithdrawal entity = new BWithdrawal();
		entity.getMap().put("size", 15);
		entity.getMap().put("begin", 0);
		entity.getMap().put("sort","state");
		List<BWithdrawalExample> bWithdrawals = bWithdrawalService.queryPageList(entity);
		Integer count = bWithdrawalService.count(entity);
		int pageSum = count % 15 == 0 ? count / 15 : (count / 15) + 1;
		model.addAttribute("bWithdrawals", bWithdrawals);
		model.addAttribute("count", count);
		model.addAttribute("pageSum", pageSum);
		model.addAttribute("pageNo", 1);
		return "admin/withdrawalApplication/list";
	}

	@RequestMapping(value = "newPage.html", method = RequestMethod.POST)
	@ResponseBody
	public Object newPage(Model model, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<>();
		BWithdrawal entity = new BWithdrawal();
		int pageNo = WebUtil.getInt(request, "pageNo", 1);
		String phone = WebUtil.getString(request, "phone", "");
		String name = WebUtil.getString(request, "name", "");
		int begin = (pageNo - 1) * 15;
		if (StringUtils.isNotBlank(name)) {
			entity.getMap().put("name", name);
		}
		if (StringUtils.isNotBlank(phone)) {
			entity.getMap().put("phone", phone);
		}
		entity.getMap().put("size", 15);
		entity.getMap().put("begin", begin);
		entity.getMap().put("sort","state");
		List<BWithdrawalExample> bWithdrawals = bWithdrawalService.queryPageList(entity);
		Integer count = bWithdrawalService.count(entity);
		int pageSum = count % 15 == 0 ? count / 15 : (count / 15) + 1;
		map.put("bWithdrawals", bWithdrawals);
		map.put("count", count);
		map.put("pageSum", pageSum);
		map.put("pageNo", pageNo);
		return map;
	}

	@RequestMapping(value = "updateState.html", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult cash(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		BWithdrawal entity = new BWithdrawal();
		entity.setId(id);
		List<BWithdrawalExample> BWithdrawal = bWithdrawalService.queryPageList(entity);
		Trans trans = new Trans();
		trans.setACCOUNT_NAME(BWithdrawal.get(0).getName());
		String count = String.valueOf((int)(BWithdrawal.get(0).getNumber() * 100));
		trans.setAMOUNT(count);
		trans.setACCOUNT_NO(BWithdrawal.get(0).getCard());
		BCard bd = bCardService.listCardByCardId(BWithdrawal.get(0).getCard());
		if (bd != null) {
			trans.setBANK_CODE(bd.getCode());
		}
		TranxMain tr = new TranxMain();
		String result = tr.idVerifyDaifu(trans);
		if (result == "") {
			return JsonResult.success("处理失败请重新提交", "");
		} else {
			String msg = result.substring(0, (result.indexOf("-")));
			String code = result.substring(result.indexOf("-") + 1);
			if (code.equals("0000")) {
				entity.setState(WithdrawStateType.处理成功.getValue());
				entity.setComTime(new Date());
				int flag = bWithdrawalService.update(entity);
				return JsonResult.success("已成功被通联受理",flag);
			} else {
				return JsonResult.success(msg, "");
			}
		}
	}

	@RequestMapping(value = "state.html", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult cash1(Model model, HttpServletRequest request) {
		String id = request.getParameter("id");
		BWithdrawal entity = new BWithdrawal();
		entity.setId(id);
		entity.setState(WithdrawStateType.驳回.getValue());
		int flag = bWithdrawalService.update(entity);
		if (flag > 0) {
			return JsonResult.success("处理成功", "");
		} else {
			return JsonResult.success("处理失败", "");
		}
	}
	
}
